package com.caifu.mapper;

import com.caifu.pojo.YlBed;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author GZ
 * @since 2020-11-05
 */
public interface YlBedMapper extends BaseMapper<YlBed> {
    /**
    * 
    * 功能描述：查询楼苑下的床位数
    * 
    * @Param buildingNo 楼苑编号  isEmpty是否空置
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/11/6 10:05
    */
    @Select({"<script>",
            "select count(*) from yl_bed b LEFT JOIN yl_room r on b.ROOM_NO = r.ROOM_NO",
            "LEFT JOIN yl_floor f on r.FLOOR_NO = f.FLOOR_NO",
            "LEFT JOIN yl_building bu on f.BUILDING_NO =  bu.BUILDING_NO",
            "WHERE b.BED_STATE =1",
            "<when test='isEmpty!=null' >",
                "and b.BED_EMPTY=#{isEmpty}",
            "</when>",
            "<when test='buildingNo!=null' >",
                "and bu.BUILDING_NO=#{buildingNo}",
            "</when>",
            "and  bu.BUILDING_STATE = 1 ",
    "</script>"})
    int queryBedCountByBuildingNo(@Param("buildingNo") String buildingNo,@Param("isEmpty") String isEmpty);
    /**
     *
     * 功能描述：查询楼苑下的床位数
     *
     * @Param buildingNo 楼苑编号  isEmpty是否空置
     * @Return
     * @Exception
     * @Author  GZ
     * @Date   2020/11/6 10:05
     */
    @Select({"<script>",
            "select max(BED_NAME) from yl_bed b LEFT JOIN yl_room r on b.ROOM_NO = r.ROOM_NO",
            "LEFT JOIN yl_floor f on r.FLOOR_NO = f.FLOOR_NO",
            "LEFT JOIN yl_building bu on f.BUILDING_NO =  bu.BUILDING_NO",
            "WHERE b.BED_STATE =1",
            "<when test='isEmpty!=null' >",
            "and b.BED_EMPTY=#{isEmpty}",
            "</when>",
            "<when test='buildingNo!=null' >",
            "and bu.BUILDING_NO=#{buildingNo}",
            "</when>",
            "and  bu.BUILDING_STATE = 1 ",
            "</script>"})
    String queryMaxBedNameByBuildingNo(@Param("buildingNo") String buildingNo,@Param("isEmpty") String isEmpty);
    /**
     *
     * 功能描述：根据楼层查询床位数量
     *
     * @Param floorNo 楼层编号  isEmpty 费否为空
     * @Return
     * @Exception
     * @Author  GZ
     * @Date   2020/11/8 11:13
     */
    @Select({"<script>",
            "select count(*) from yl_bed b LEFT JOIN yl_room r on b.ROOM_NO = r.ROOM_NO",
            "LEFT JOIN yl_floor f on r.FLOOR_NO = f.FLOOR_NO",
            "WHERE b.BED_STATE =1",
            "<if test='isEmpty!=null ' >",
                "and b.BED_EMPTY = #{isEmpty}",
            "</if>",
            "<if test='floorNo!=null' >",
            "and f.FLOOR_NO = #{floorNo}",
            "</if>",
            "and  f.FLOOR_STATE = 1 ",
            "</script>"})
    int queryBedCountByFloorNo(@Param("floorNo") String floorNo,@Param("isEmpty") String isEmpty);
    /**
     *
     * 功能描述：根据房间查询床位数量
     *
     * @Param roomNo 房间编号  isEmpty 费否为空
     * @Return
     * @Exception
     * @Author  GZ
     * @Date   2020/11/8 11:13
     */
    @Select({"<script>",
            "select count(*) from yl_bed b LEFT JOIN yl_room r on b.ROOM_NO = r.ROOM_NO",
            "WHERE b.BED_STATE =1",
            "<if test='isEmpty!=null ' >",
            "and b.BED_EMPTY = #{isEmpty}",
            "</if>",
            "<if test='roomNo!=null' >",
            "and r.ROOM_NO = #{roomNo}",
            "</if>",
            "and r.ROOM_STATE = 1 ",
            "</script>"})
    int queryBedCountByRoomNo(@Param("roomNo") String roomNo,@Param("isEmpty") String isEmpty);
    /**
    * 
    * 功能描述：删除房间下的所有床位
    * 
    * @Param
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/11/8 19:53
    */
    @Select({"<script>",
                " update yl_bed set BED_STATE='9' where ROOM_NO=#{roomNo}",
            "</script>"})
    void deleteBedInfoByRoomNo(@Param("roomNo") String roomNo);
    /**
    * 
    * 功能描述：查询床位信息
    * 
    * @Param roomNo 房间编号 bedName 床位名称
    * @Return
    * @Exception
    * @Author  GZ
    * @Date   2020/11/8 18:48
    */
    @Select({"<script>",
            " select count(*) from yl_bed b left join yl_room r on b.ROOM_NO=r.ROOM_NO LEFT JOIN yl_floor f",
            "on r.FLOOR_NO = f.FLOOR_NO left join yl_building bu on bu.BUILDING_NO=f.BUILDING_NO",
            "where bu.BUILDING_NO=#{buildingNo} and b.BED_NAME=#{bedName}",
            "and BED_STATE =1",
            "</script>"})
    int queryBedCountByBuildingNoAndBedName(@Param("buildingNo") int buildingNo,@Param("bedName") String bedName);
    /**
    * 
    * 功能描述：根据养老机构编号查询房间数量
    * 
    * @Param pensionNo 养老机构编号  isEmpty 是否为空床位
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/11/24 14:20
    */
    @Select({"<script>",
            "select count(*) from yl_bed b LEFT JOIN yl_room r on b.ROOM_NO = r.ROOM_NO",
            "LEFT JOIN yl_floor f on r.FLOOR_NO = f.FLOOR_NO",
            "LEFT JOIN yl_building bu on f.BUILDING_NO =  bu.BUILDING_NO",
            "WHERE b.BED_STATE =1",
            "<when test='isEmpty==0' >",
                "and b.BED_EMPTY=#{isEmpty}",
                "and r.ROOM_TYPE=101",
            "</when>",
            "<when test='pensionNo!=null' >",
            "and bu.PENSION_NO=#{pensionNo}",
            "</when>",
            "and  bu.BUILDING_STATE = 1 ",
            "</script>"})
    int queryTotalBedInfoByPensionNo(@Param("pensionNo") String pensionNo,@Param("isEmpty") String isEmpty) throws Exception;
    /**
    * 
    * 功能描述：查询已使用的床位数量
    * 
    * @Param roomNo 房间编号
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/12/4 18:11
    */
    @Select({"<script>",
            "select count(*) from yl_bed b ",
            "where b.BED_EMPTY in(1,2) ",
            "and b.ROOM_NO = #{roomNo} ",
            "</script>"})
    int queryUserBendCount(@Param("roomNo") String roomNo);
    /**
    * 
    * 功能描述：根据床位编号查询楼苑编号
    * 
    * @Param bedNo 床位编号
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/12/6 15:35
    */
    int queryBuildingNoByBedNo(@Param("bedNo") int bedNo);
    /**
    * 
    * 功能描述：根据房间编号和床位名称查询床位数量
    * 
    * @Param
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/12/17 16:52
    */
    int queryBedCountByBedNameAndRoomNo(@Param("roomNo") String roomNo,@Param("bedName") String bedName);
}
